﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using DBUtility;
namespace OracleDAL.Sec
{
    /// <summary>
    /// 数据访问类UserGroupOperate。
    /// </summary>
    public class UserGroupOperate
    {
        public UserGroupOperate()
        { 
             
        }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string USERGROUPISN, string BELONGSYSTEM, string OPERATEID, int ENABLED)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from SEC_USERGROUP_OPERATE");
            strSql.Append(" where USERGROUPISN=:USERGROUPISN and BELONGSYSTEM=:BELONGSYSTEM and OPERATEID=:OPERATEID and ENABLED=:ENABLED ");
            OracleParameter[] parameters = {
					new OracleParameter(":USERGROUPISN", OracleType.VarChar,50),
					new OracleParameter(":BELONGSYSTEM", OracleType.VarChar,50),
					new OracleParameter(":OPERATEID", OracleType.VarChar,50),
					new OracleParameter(":ENABLED", OracleType.Number,4)};
            parameters[0].Value = USERGROUPISN;
            parameters[1].Value = BELONGSYSTEM;
            parameters[2].Value = OPERATEID;
            parameters[3].Value = ENABLED;

            return OracleHelper.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Model.Sec.UserGroupOperate model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into SEC_USERGROUP_OPERATE(");
            strSql.Append("USERGROUPISN,BELONGSYSTEM,OPERATEID,ENABLED)");
            strSql.Append(" values (");
            strSql.Append(":USERGROUPISN,:BELONGSYSTEM,:OPERATEID,:ENABLED)");
            OracleParameter[] parameters = {
					new OracleParameter(":USERGROUPISN", OracleType.VarChar,50),
					new OracleParameter(":BELONGSYSTEM", OracleType.VarChar,10),
					new OracleParameter(":OPERATEID", OracleType.VarChar,50),
					new OracleParameter(":ENABLED", OracleType.Number,4)};
            parameters[0].Value = model.USERGROUPISN;
            parameters[1].Value = model.BELONGSYSTEM;
            parameters[2].Value = model.OPERATEID;
            parameters[3].Value = model.ENABLED;

            OracleHelper.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Model.Sec.UserGroupOperate model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SEC_USERGROUP_OPERATE set ");
            strSql.Append("USERGROUPISN=:USERGROUPISN,");
            strSql.Append("BELONGSYSTEM=:BELONGSYSTEM,");
            strSql.Append("OPERATEID=:OPERATEID,");
            strSql.Append("ENABLED=:ENABLED");
            strSql.Append(" where USERGROUPISN=:USERGROUPISN and BELONGSYSTEM=:BELONGSYSTEM and OPERATEID=:OPERATEID and ENABLED=:ENABLED ");
            OracleParameter[] parameters = {
					new OracleParameter(":USERGROUPISN", OracleType.VarChar,50),
					new OracleParameter(":BELONGSYSTEM", OracleType.VarChar,10),
					new OracleParameter(":OPERATEID", OracleType.VarChar,50),
					new OracleParameter(":ENABLED", OracleType.Number,4)};
            parameters[0].Value = model.USERGROUPISN;
            parameters[1].Value = model.BELONGSYSTEM;
            parameters[2].Value = model.OPERATEID;
            parameters[3].Value = model.ENABLED;

            OracleHelper.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(string USERGROUPISN, string BELONGSYSTEM, string OPERATEID, int ENABLED)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete SEC_USERGROUP_OPERATE ");
            strSql.Append(" where USERGROUPISN=:USERGROUPISN and BELONGSYSTEM=:BELONGSYSTEM and OPERATEID=:OPERATEID and ENABLED=:ENABLED ");
            OracleParameter[] parameters = {
					new OracleParameter(":USERGROUPISN", OracleType.VarChar,50),
					new OracleParameter(":BELONGSYSTEM", OracleType.VarChar,50),
					new OracleParameter(":OPERATEID", OracleType.VarChar,50),
					new OracleParameter(":ENABLED", OracleType.Number,4)};
            parameters[0].Value = USERGROUPISN;
            parameters[1].Value = BELONGSYSTEM;
            parameters[2].Value = OPERATEID;
            parameters[3].Value = ENABLED;

            OracleHelper.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Sec.UserGroupOperate GetModel(string USERGROUPISN, string BELONGSYSTEM, string OPERATEID, int ENABLED)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select USERGROUPISN,BELONGSYSTEM,OPERATEID,ENABLED from SEC_USERGROUP_OPERATE ");
            strSql.Append(" where USERGROUPISN=:USERGROUPISN and BELONGSYSTEM=:BELONGSYSTEM and OPERATEID=:OPERATEID and ENABLED=:ENABLED ");
            OracleParameter[] parameters = {
					new OracleParameter(":USERGROUPISN", OracleType.VarChar,50),
					new OracleParameter(":BELONGSYSTEM", OracleType.VarChar,50),
					new OracleParameter(":OPERATEID", OracleType.VarChar,50),
					new OracleParameter(":ENABLED", OracleType.Number,4)};
            parameters[0].Value = USERGROUPISN;
            parameters[1].Value = BELONGSYSTEM;
            parameters[2].Value = OPERATEID;
            parameters[3].Value = ENABLED;

            Model.Sec.UserGroupOperate model = new Model.Sec.UserGroupOperate();
            DataSet ds = OracleHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.USERGROUPISN = ds.Tables[0].Rows[0]["USERGROUPISN"].ToString();
                model.BELONGSYSTEM = ds.Tables[0].Rows[0]["BELONGSYSTEM"].ToString();
                model.OPERATEID = ds.Tables[0].Rows[0]["OPERATEID"].ToString();
                if (ds.Tables[0].Rows[0]["ENABLED"].ToString() != "")
                {
                    model.ENABLED = int.Parse(ds.Tables[0].Rows[0]["ENABLED"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select USERGROUPISN,BELONGSYSTEM,OPERATEID,ENABLED ");
            strSql.Append(" FROM SEC_USERGROUP_OPERATE ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return OracleHelper.Query(strSql.ToString());
        }
        /// <summary>
        /// 返回用户组BS的权限
        /// </summary>
        /// <param name="UserGroupISN"></param>
        /// <returns>返回的OperateID以","分开</returns>
        public string GetOperateID(string UserGroupISN)
        {
            string sql = "select distinct(operateid) from sec_usergroup_operate where enabled='1' and belongsystem='10' and usergroupisn='" + UserGroupISN + "'";
            OracleDataReader dr = OracleHelper.ExecuteReader(sql);
            string strOperateID=string.Empty;
            while (dr.Read())
                strOperateID += dr["operateid"].ToString() + ",";
            return strOperateID;
        }
        /// <summary>
        /// 返回用户组BS的权限但不包括按钮,返回的OperateID以","分开
        /// </summary>
        /// <param name="UserGroupISN"></param>
        /// <returns>返回的OperateID以","分开</returns>
        public string GetOperateIDExceptButton(string UserGroupISN)
        {
            string sql = "select distinct(u.operateid) as operate,treeorder from sec_usergroup_operate u,sec_operate_new o  where enabled='1' and u.belongsystem='10' and usergroupisn='" + UserGroupISN + "'";
            sql += " and u.operateid=o.operateid and o.belongsystem='10' and (treecatalog <>'button' or treecatalog is null) order by to_number(treeorder)";
            OracleDataReader dr = OracleHelper.ExecuteReader(sql);
            string strOperateID = string.Empty;
            while (dr.Read())
                strOperateID += dr["operate"].ToString() + ",";
            return strOperateID;
        }
        /// <summary>
        /// 获取用户组下一级有权限的OperateID
        /// </summary>
        /// <param name="ParentTreeOrder"></param>
        /// <param name="UserGroupISN"></param>
        /// <returns>返回的OperateID以","分开</returns>
        public string getChildrenOperateID(string ParentTreeOrder, string UserGroupISN)
        {
            string sql = "select distinct(u.operateid) as operate,treeorder from sec_usergroup_operate u,sec_operate_new o where enabled='1' and u.belongsystem='10' and usergroupisn='" + UserGroupISN + "'";
            sql += " and o.belongsystem='10' and o.operateid=u.operateid and o.treeorder like'" + ParentTreeOrder + "__' order by to_number(treeorder)";
            OracleDataReader dr = OracleHelper.ExecuteReader(sql);
            string strOperateID = string.Empty;
            while (dr.Read())
                strOperateID += dr["operate"].ToString() + ",";
            return strOperateID;
        }
        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            OracleParameter[] parameters = {
                    new OracleParameter(":tblName", OracleType.VarChar, 255),
                    new OracleParameter(":fldName", OracleType.VarChar, 255),
                    new OracleParameter(":PageSize", OracleType.Number),
                    new OracleParameter(":PageIndex", OracleType.Number),
                    new OracleParameter(":IsReCount", OracleType.Clob),
                    new OracleParameter(":OrderType", OracleType.Clob),
                    new OracleParameter(":strWhere", OracleType.VarChar,1000),
                    };
            parameters[0].Value = "SEC_USERGROUP_OPERATE";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return OracleHelper.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}
